# -*- coding: utf-8 -*-
# created on 2016/12/21

from sympy import Intersection, sympify
from mathsolver.functions.base import BaseFunction, new_latex
from mathsolver.functions.base import BasePoly
from mathsolver.functions.hanshu.dingyiyu import QiuDingYiYu


class HanShuYunSuanQiuJieXiShi(BaseFunction):
    """函数运算求函数解析式"""

    def solver(self, *args):
        func1, func2, poly = args
        expr = sympify(poly.value)

        # 求定义域
        dingyiyu1 = QiuDingYiYu().solver(func1)
        dingyiyu2 = QiuDingYiYu().solver(func2)
        dingyiyu = Intersection(dingyiyu1, dingyiyu2)

        self.steps.append(["%s 的定义域为 %s" % (func1.printing(), new_latex(dingyiyu1)),
                           "%s 的定义域为 %s" % (func2.printing(), new_latex(dingyiyu2))])
        self.steps.append(["", "所以 %s 的定义域为 %s" % (new_latex(expr), new_latex(dingyiyu))])

        # 替换
        tihuan = {sympify(str(func1.name) + '(' + str(func1.var) + ')'): func1.expression,
                  sympify(str(func2.name) + '(' + str(func2.var) + ')'): func2.expression}

        res = expr.subs(tihuan)
        self.steps.append(
            ["", "将 %s 和 %s 代入得 %s = %s" % (func1.printing(), func2.printing(), str(expr), str(res))])
        self.output.append(BasePoly(res))
        self.label.add("函数运算求函数解析式")
        return self


if __name__ == '__main__':
    pass
